@tool
class_name PropertyInfoBox
extends PanelContainer

#region 属性
@export var property_name_edit:LineEdit
@export var fold_v_box_container:VBoxContainer
@export_group("Button")
@export var fold_btn:TextureButton
@export var remove_btn:TextureButton
@export var property_type_obtn:OptionButton
@export_group("")
@export_group("Property")
@export var property_value_string:LineEdit
@export var property_value_int:SpinBox
@export var property_value_float:SpinBox
@export var property_value_bool:CheckBox
@export_group("")
var visual_view:VisualView
var skill:Skill
var init_data:Dictionary
var property_name:String
var connect_obj_dict:Dictionary[Signal,ConnectObj] = {}
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init(visual_view:VisualView,skill:Skill,property_name:String):
	self.visual_view = visual_view
	self.skill = skill
	self.init_data = self.skill.init_data
	self.property_name = property_name
	self.property_type_obtn.clear()
	for t in GameEnum.SkillPropertyType.keys():
		self.property_type_obtn.add_item(t)
	connect_obj_dict[property_name_edit.text_changed] = ConnectObj.new(property_name_edit.text_changed,[on_property_name_changed])
	connect_obj_dict[fold_btn.pressed] = ConnectObj.new(fold_btn.pressed,[on_fold_btn_pressed])
	connect_obj_dict[remove_btn.pressed] = ConnectObj.new(remove_btn.pressed,[on_remove_btn_pressed])
	connect_obj_dict[property_type_obtn.item_selected] = ConnectObj.new(property_type_obtn.item_selected,[on_property_type_changed])
	connect_obj_dict[property_value_string.text_changed] = ConnectObj.new(property_value_string.text_changed,[on_property_value_string_changed])
	connect_obj_dict[property_value_int.value_changed] = ConnectObj.new(property_value_int.value_changed,[on_property_value_int_changed])
	connect_obj_dict[property_value_float.value_changed] = ConnectObj.new(property_value_float.value_changed,[on_property_value_float_changed])
	connect_obj_dict[property_value_bool.toggled] = ConnectObj.new(property_value_bool.toggled,[on_property_value_bool_changed])
	fold_v_box_container.visible = false
	update_property_value()
func get_value():
	return init_data[property_name]
func set_value(new_value):
	init_data.set(property_name,new_value)
func update_property_value():
	property_value_string.visible = false
	property_value_string.text = ""
	property_value_int.visible = false
	property_value_int.value = 0
	property_value_float.visible = false
	property_value_float.value = 0.0
	property_value_bool.visible = false
	property_value_bool.button_pressed = false
	property_name_edit.text = property_name
	var value = get_value()
	match typeof(value):
		TYPE_STRING:
			property_value_string.text = value
			property_value_string.visible = true
			property_type_obtn.select(0)
			return
		TYPE_INT:
			property_value_int.value = value
			property_value_int.visible = true
			property_type_obtn.select(1)
			return
		TYPE_FLOAT:
			property_value_float.value = value
			property_value_float.visible = true
			property_type_obtn.select(2)
			return
		TYPE_BOOL:
			property_value_bool.button_pressed = value
			property_value_bool.visible = true
			property_type_obtn.select(3)
			return
func destroy():
	queue_free()
#region 事件回调
## PropertyName修改
func on_property_name_changed(new_text:String):
	var value = get_value()
	init_data.erase(property_name)
	property_name = new_text
	set_value(value)
	update_property_value()
	visual_view.property_name_changed.emit()
## 折叠按钮点击
func on_fold_btn_pressed():
	fold_v_box_container.visible = not fold_v_box_container.visible
## 删除按钮点击
func on_remove_btn_pressed():
	init_data.erase(property_name)
	destroy()
	visual_view.property_removed.emit(property_name)
## 类型切换
func on_property_type_changed(index:int):
	var type = property_type_obtn.get_item_text(index)
	if type == Utils.get_enum_key_by_value(GameEnum.SkillPropertyType,GameEnum.SkillPropertyType.String):
		if typeof(get_value()) == TYPE_STRING:
			return
		set_value("")
	elif type == Utils.get_enum_key_by_value(GameEnum.SkillPropertyType,GameEnum.SkillPropertyType.Int):
		if typeof(get_value()) == TYPE_INT:
			return
		set_value(0)
	elif type == Utils.get_enum_key_by_value(GameEnum.SkillPropertyType,GameEnum.SkillPropertyType.Float):
		if typeof(get_value()) == TYPE_FLOAT:
			return
		set_value(0.0)
	elif type == Utils.get_enum_key_by_value(GameEnum.SkillPropertyType,GameEnum.SkillPropertyType.Bool):
		if typeof(get_value()) == TYPE_BOOL:
			return
		set_value(false)
	elif type == Utils.get_enum_key_by_value(GameEnum.SkillPropertyType,GameEnum.SkillPropertyType.Enum):
		var info = Utils
		set_value(false)
	update_property_value()
	visual_view.property_type_changed.emit()
## String值修改
func on_property_value_string_changed(new_text: String):
	set_value(new_text)
	visual_view.property_value_changed.emit(new_text)
## Int值修改
func on_property_value_int_changed(value:int):
	set_value(value)
	visual_view.property_value_changed.emit(value)
## Float值修改
func on_property_value_float_changed(value:float):
	set_value(value)
	visual_view.property_value_changed.emit(value)
## Bool值修改
func on_property_value_bool_changed(toggled_on: bool):
	set_value(toggled_on)
	visual_view.property_value_changed.emit(toggled_on)
#endregion
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _notification(what: int) -> void:
	if what == NOTIFICATION_PREDELETE:
		for connect_obj:ConnectObj in connect_obj_dict.values():
			if connect_obj:
				connect_obj.disconnect_all()
#endregion
